/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package drexel.cs544.nap.server.xml;

import drexel.cs544.nap.server.Group;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.Stack;

import java.util.Vector;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
 *
 * @author rch_ltp
 */
public class GroupLoader extends DefaultHandler
{
    private Vector<Group> _groups;
    private Stack<String> _tagNames;

    private SaxDocument _sax;
    private StringBuffer _sb = new StringBuffer();

    public GroupLoader()
    {
        super(); 
        _tagNames = new Stack<String>();
        _groups = new Vector<Group>();
    }

    public static GroupLoader getInstance()
    {
        return new GroupLoader();
    }

    //=================
    // parse()
    //=================

    public Vector<Group> parse(File xml) throws IOException, SAXException
    {
        return parse(xml.getAbsolutePath());
    }

    public Vector<Group> parse(String url) throws IOException, SAXException
    {
    	_sb = new StringBuffer(); 
        _sax = new SaxDocument(url, this);
        _sax.parse();
        _sax.close();

        return _groups;
    } 

    //===============
    // startElement()
    //===============

    @Override
    public void startElement(String namespaceURI, String localName, String qualifiedName, Attributes attributes)
    {
         if (localName.equalsIgnoreCase("group"))
             _groups.add(new Group());
        _tagNames.push(localName);
         _sb = new StringBuffer();
    }

    //===============
    // characters()
    //===============

    @Override
    public void characters(char[] content, int start, int length)
    {
        String theString = new String(content, start, length);
        _sb.append(theString);
    }

      /*<GroupID>1</GroupID>
        <Datamode>1</Datamode>
        <Maxdataset>500</Maxdataset>
        <XMLFileDirectory>c:/temp/</XMLFileDirectory>
        <Timeout>0</Timeout>*/
    @Override
    public void endElement (String uri, String localName, String qName) throws SAXException
    {
        try
        {
            if(!_tagNames.empty())
            {
                String tag = _tagNames.pop();
                String data = _sb.toString();
                data = (data.equalsIgnoreCase("NULL")) ? null : data;
                if (tag.equalsIgnoreCase("GroupID"))
                {
                    int groupID = Integer.parseInt(data);
                    _groups.lastElement().setGroupID(groupID);
                }
                else if (tag.equalsIgnoreCase("Datamode"))
                {
                    long dataMode = Long.parseLong(data);
                    _groups.lastElement().setDataMode(dataMode);
                }
                else if (tag.equalsIgnoreCase("Maxdataset"))
                {
                    long maxDataSet = Long.parseLong(data);
                    _groups.lastElement().setMaxDataSet(maxDataSet);
                }
                else if (tag.equalsIgnoreCase("XMLFileDirectory"))
                    _groups.lastElement().setXmlLocation(data);
                else if (tag.equalsIgnoreCase("Timeout"))
                {
                    long timeout = Long.parseLong(data);
                    _groups.lastElement().setTimeout(timeout);
                }
                                
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
            throw new SAXException(ex);
        }
    }
     
}
